package com.sss.ordermodel.feignservice;

import com.sss.common.entities.Product;
import com.sss.common.responseutil.Result;
import io.seata.common.util.StringUtils;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.tm.api.GlobalTransactionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 服务降级消息返回
 * @author sss
 * @date 2020-04-06 16:16
 **/
@Component
public class ProductServiceImpl implements ProductService {
    private static final Logger log = LoggerFactory
            .getLogger(ProductServiceImpl.class);
    @Override
    public Result<List<Product>> productList() {
        log.info("********解决Feignfallback降级后事务不会滚问题*****");
        try {
            //TransactionManagerHolder.get().rollback(RootContext.getXID());
            if (!StringUtils.isBlank(RootContext.getXID())){
                GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            }
        } catch (TransactionException e) {
            log.error(e.getMessage(),e);
        }
        return Result.serverBusy();
    }

    @Override
    public Result<Boolean> productUpdate(Integer id, Product product) {
        log.info("********解决Feignfallback降级后事务不会滚问题*****");
        try {
            //TransactionManagerHolder.get().rollback(RootContext.getXID());
            if (!StringUtils.isBlank(RootContext.getXID())){
                GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            }
        } catch (TransactionException e) {
            log.error(e.getMessage(),e);
        }
        return Result.serverBusy();
    }

    @Override
    public Result<Product> getProductById(Integer id) {
        log.info("********解决Feignfallback降级后事务不会滚问题*****");
        try {
            //TransactionManagerHolder.get().rollback(RootContext.getXID());
            if (!StringUtils.isBlank(RootContext.getXID())) {
                GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            }
        } catch (TransactionException e) {
            log.error(e.getMessage(),e);
        }
        return Result.serverBusy();
    }
}
